package com.zcbl.toolkit.map.baidu.direction;

import com.zcbl.toolkit.map.baidu.ApiJsonResponse;
import com.zcbl.toolkit.map.baidu.coord.Location;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @author he peng
 * @create 2018/4/24 11:12
 * @see
 */

@Data
public class RidingRouteJsonResponse extends ApiJsonResponse {

    private RidingRouteResult result;

    public Route getFirstRoute() {
        if (Objects.isNull(this.result) || CollectionUtils.sizeIsEmpty(this.result.getRoutes())) {
            return null;
        }
        return this.result.getRoutes().get(0);
    }

    public List<Location> getRouteCoordinatePoints() {
        Route route = getFirstRoute();
        if (Objects.isNull(route)) {
            return null;
        }
        List<Location> locations = new ArrayList<>(20);
        for (RouteStep routeStep : route.getSteps()) {
            if (StringUtils.isBlank(routeStep.getPath())) {
                continue;
            }
            for (String s : StringUtils.split(routeStep.getPath(), ";")) {
                String[] coord = StringUtils.split(s, ",");
                if (Objects.nonNull(coord) && coord.length == 2) {
                    Location location = new Location();
                    location.setLongitude(Double.valueOf(coord[0]));
                    location.setLatitude(Double.valueOf(coord[1]));
                    locations.add(location);
                }
            }
        }
        return locations;
    }
}
